
Ext.define('ZO0.store.filter.Vendors', {
    extend: 'Ext.data.Store',

    model: 'ZO0.model.filter.Vendors',
    //autoDestroy: true,

    proxy: {
        type: 'ajax',
        api: {
            read: 'what?do=google&ex=getVendors',
            update: 'what?do=google&ex=updateVendors'
        },
        reader: {
            type: 'json',
            root: 'data',
            successProperty: 'success'
        }
        //writer: {
        //    writeAllFields: false,
        //    type: 'json',
        //    root: 'data'
        //}
//        rowEditing.on('edit', function(editor, e, eOpts) {
//        var store = editor.grid.getStore();
//        var filters = editor.grid.getStore().filters;
//        for(var i=0; i<filters.length; i++)
//        store.filter(filters[i]);
//        });

    },
//    datachanged: function(grid, eOpts) {
//        console.info('grid=',grid);
//    },
    createSubcategoryTable: function(catname) {

        var json = this.getProxy().getReader().jsonData.data,
            data = new Ext.util.HashMap(), head = new Ext.util.HashMap(), idx, cid, vid, str;
        head.add(0, { xtype: 'gridcolumn', header: '&nbsp;' + catname, dataIndex: 'cname', width: 200, locked: true });
        for(idx in json) {

            cid = json[idx].cid;
            vid = json[idx].vid;
            if(!data.containsKey(cid)) {
                str = '{cid:cid,cname:json[idx].cname' + this.getColumn(cid) + '}';
                data.add(cid, eval('(' + str + ')'));
            }

            if(!head.containsKey(vid)) {
                head.add(vid, {
                    xtype: 'mychclmn',
                    width: 40,
                    header: json[idx].alias,
                    dataIndex: json[idx].alias.toLowerCase(),
                    sortable: false,
                    menuDisabled: true,
                    tooltip: json[idx].vname
                });
            }
        }
        //console.info('dataMap.V=',dataMap.getValues());
        //console.info('headMap.getV=',headMap.getValues());

        //var store2 = Ext.getStore('filter.Vendors');
        //console.log('store2=',store2);
        //console.log('this=',this);
        //this.removeAll();
        //this.insert(0, data.getValues());
        Ext.suspendLayouts();
        var grid = Ext.ComponentQuery.query('vendorscategory')[0];
        grid.reconfigure(
            new Ext.data.Store({
                autoSync: true,
                model: 'ZO0.model.filter.Vendors',
                proxy: {
                    type: 'ajax',
                    api: {
                        update: 'what?do=google&ex=updateVendors'
                    },
                    writer: {
                        writeAllFields: false,
                        type: 'json',
                        root: 'data'
                    }
                },
                data: data.getValues()
            }),
            //this,
            head.getValues()
        );
        Ext.resumeLayouts(true);
    },
    getColumn: function(cid) {
        var json = this.getProxy().getReader().jsonData.data, idx, active, str = '';
        for(idx in json) {
            if(cid === json[idx].cid) {
                active = this.isActive(cid, json[idx].vid);
                str += ',' + json[idx].alias.toLowerCase() + ':' + active;
            }
        }
        return str;
    },
    isActive: function(cid, vid) {
        var json = this.getProxy().getReader().jsonData.group, idx;
        for(idx in json) {
            if(cid === json[idx].cid && vid === json[idx].vid) {
                return true;
            }
        }
        return false;
    }
});
